package com.zenchn.service.impl;

import com.zenchn.dao.TSysAuthorityMapper;
import com.zenchn.dao.TSysRoleAuthorityMapperMapper;
import com.zenchn.dao.TSysRoleMapper;
import com.zenchn.dynamic.TSysRoleAuthorityMapperDynamicMapper;
import com.zenchn.generic.GenericDao;
import com.zenchn.generic.GenericServiceImpl;
import com.zenchn.model.*;
import com.zenchn.service.RoleAuthorityMapperService;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static org.mybatis.dynamic.sql.SqlBuilder.*;
//import static com.zenchn.dynamic.TSysRoleAuthorityMapperDynamicSqlSupport.*;
/**
 * 角色权限 业务实现类
 *
 * @author dingxinfa
 * @date 2020-04-16
 **/
@Service
@Transactional(rollbackFor = Exception.class)
public class RoleAuthorityMapperServiceImpl extends GenericServiceImpl<TSysRoleAuthorityMapper, String, TSysRoleAuthorityMapperExample> implements RoleAuthorityMapperService {

    @Resource
    private TSysRoleAuthorityMapperMapper roleAuthorityMapperMapper;

    @Resource
    private TSysAuthorityMapper authorityMapper;

    @Resource
    private TSysRoleMapper roleMapper;

    @Resource
    private TSysRoleAuthorityMapperDynamicMapper roleAuthorityMapperDynamicMapper;

    @Override
    public GenericDao<TSysRoleAuthorityMapper, String, TSysRoleAuthorityMapperExample> getDao() {
        return roleAuthorityMapperMapper;
    }

    @Override
    public List<TSysAuthority> selectAuthorityByRoleId(String roleId) {
        return roleAuthorityMapperMapper.selectAuthorityByRoleId(roleId);
    }

    @Override
    public List<TSysAuthority> selectAuthorityByRoleIds(List<String> roleIds) {
        TSysRoleAuthorityMapperExample roleAuthorityExample=new TSysRoleAuthorityMapperExample();
        roleAuthorityExample.createCriteria().andRoleIdIn(roleIds);
        List<TSysRoleAuthorityMapper> roleAuthorityList=roleAuthorityMapperMapper.selectByExample(roleAuthorityExample);

        List<String> authIds=roleAuthorityList.stream().map(TSysRoleAuthorityMapper::getAuthId).distinct().collect(Collectors.toList());
        if (!authIds.isEmpty()) {
            TSysAuthorityExample authorityExample = new TSysAuthorityExample();
            authorityExample.createCriteria().andAuthIdIn(authIds);
            return authorityMapper.selectByExample(authorityExample);
        }
        return null;
    }

    @Override
    public List<String> selectFullAuthIdbyRoleId(String roleId) {
        TSysRoleAuthorityMapperExample roleAuthorityExample=new TSysRoleAuthorityMapperExample();
        roleAuthorityExample.createCriteria().andRoleIdEqualTo(roleId);
        List<TSysRoleAuthorityMapper> roleAuthorityList=roleAuthorityMapperMapper.selectByExample(roleAuthorityExample);

        return  roleAuthorityList.stream().map(TSysRoleAuthorityMapper::getAuthId).distinct().collect(Collectors.toList());
    }

    @Override
    public int updateRoleAuth(String roleId, List<String> authIdList) throws Exception{
        int total = 0;
        if (null!=roleId) {
            TSysRoleAuthorityMapperExample roleAuthorityExample = new TSysRoleAuthorityMapperExample();
            roleAuthorityExample.createCriteria().andRoleIdEqualTo(roleId);
            roleAuthorityMapperMapper.deleteByExample(roleAuthorityExample);

            if (null != authIdList && !authIdList.isEmpty()) {
                for (String authId : authIdList) {
                    TSysRoleAuthorityMapper roleAuthority = new TSysRoleAuthorityMapper();
                    roleAuthority.setAuthId(authId);
                    roleAuthority.setRoleId(roleId);
                    roleAuthorityMapperMapper.insert(roleAuthority);
                    total++;
                }
            }
        }
        return total;
    }
}
